<script setup >
import { ref, reactive } from "vue";
import { NModal, NCard, NButton, useMessage, NSpin } from "naive-ui";
import { cancelReason } from "@/api/constants";
import { cancelOrder } from "@/api/order";
const message = useMessage();

defineExpose({
  open(order) {
    cancelOrderInfo.value = order;
    visibleDialog.value = true;
  },
});
const showSpin = ref(false);
const cancelOrderInfo = ref(null);
let visibleDialog = ref(false);

// 选中的取消原因
const curText = ref("");

// 提交取消订单
const submit = () => {
  if (!curText.value) return message.warning("请选择取消原因");
  showSpin.value = true;
  cancelOrder(cancelOrderInfo.value.id, curText.value).then(() => {
    curText.value = "";
    cancelOrderInfo.value.orderState = 6;
    message.success("取消成功！");
    showSpin.value = true;
    visibleDialog.value = false;
  });
};
</script>

<template>
  <div>
    <n-modal v-model:show="visibleDialog">
      <n-card
        style="width: 600px"
        title="取消订单"
        :bordered="false"
        size="huge"
        role="dialog"
        aria-modal="true"
      >
        <n-spin :show="showSpin">
          <div class="cancel-info">
            <p>取消订单后，本单享有的优惠可能会一并取消，是否继续？</p>
            <p class="tip">请选择取消订单的原因（必选）：</p>
            <div class="btn">
              <a
                @click="curText = item"
                v-for="item in cancelReason"
                :key="item"
                href="javascript:;"
                :class="{ active: curText === item }"
              >
                {{ item }}
              </a>
            </div>
          </div>
        </n-spin>
        <template #footer>
          <n-button type="primary" @click="submit">确认</n-button>
          <n-button
            type="default"
            @click="visibleDialog = false"
            style="margin-left: 20px"
            >取消</n-button
          >
        </template>
      </n-card>
    </n-modal>
  </div>
</template>

<style scoped lang='less'>
.cancel-info {
  p {
    font-size: 15px;
    line-height: 35px;
    &.tip {
      font-size: 14px;
      color: #999;
    }
  }
  .btn {
    padding-top: 21px;
    display: flex;
    flex-wrap: wrap;
    a {
      width: 250px;
      height: 45px;
      line-height: 45px;
      text-align: center;
      background-color: #ffffff;
      border: 1px solid #e4e4e4;
      margin-right: 20px;
      margin-bottom: 20px;
      color: #666;
      &:nth-child(2n) {
        margin-right: 0;
      }
      &:hover,
      &.active {
        background-color: #e3f9f4;
        border-color: @xtxColor;
      }
    }
  }
}
</style>